/**
 * [OElove] (C)2010-2099 OELOVE.COM Inc. X
 * Email：service@phpcoo.com，phpcoo@qq.com
 * This is NOT a freeware, use is subject to license terms
 * $ Last update 2017/04/07 by OEdev $
*/

//获取select选框子地区$obj->当前select的名称 不含 depth
function oeloveGetChildArea($jname, $depth, $child, $text, $callback) {
	if (typeof($child) == "undefined") {$child = 0;}
	if (typeof($text) == "undefined") {$text = "";}
	if (typeof($callback) == "undefined") {$callback = "";}
	$depth = parseInt($depth);
	
	if ($depth < 4) {
		$j_obj = $("#"+$jname+$depth); //当前SELECT的OBJ
		$rootid = $j_obj.find("option:selected").val(); //当前SELECT所选的值

		if ($depth > 0) {
			if ($rootid < 1) { //加载子地区必选父地区
				//清空depth以上的SELECT
				if ($depth == 1) {
					$("#json_"+$jname+($depth+1)).html("<select name='"+$jname+"2'><option value=''>"+$text+"</option></select>"); //2
					$("#json_"+$jname+($depth+2)).html("<select name='"+$jname+"3'><option value=''>"+$text+"</option></select>"); //3
					$("#json_"+$jname+($depth+3)).html(""); //4
				}
				else if ($depth == 2) {
					$("#json_"+$jname+($depth+1)).html("<select name='"+$jname+"3'><option value=''>"+$text+"</option></select>"); //3
					$("#json_"+$jname+($depth+2)).html(""); //4
				}
				else {
					$("#json_"+$jname+($depth+1)).html(""); //4
				}
				return;
			}
		}
		//获取子地区
		$.ajax({
			type: "POST",
			url: _ROOT_PATH+"index.php",
			cache: false,
			data: {m:"index", c:"area", a:"child", parentid:$rootid, depth:$depth, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$option_html = "";
				$.each($data, function($entryIndex, $obj){
					$option_html += "<option value='"+$obj.areaid+"'>"+$obj.areaname+"</option>";
				});
				//HTML
				$child_name = $jname+""+($depth+1);
				$html_result = "";
				if ($option_html.length > 0) { //存在option
					$html_result += "<select name='"+$child_name+"' id='"+$child_name+"'";
					if ($child > 0) {
						
						if ($callback.length > 0) {
							$html_result += " onchange=\"oeloveGetChildArea('"+$jname+"', '"+($depth+1)+"', '"+$child+"', '"+$text+"', '"+$callback+"')\"";
						}
						else {
							$html_result += " onchange=\"oeloveGetChildArea('"+$jname+"', '"+($depth+1)+"', '"+$child+"', '"+$text+"')\"";
						}
					}
					$html_result +=">";
					$html_result += "<option value=''>"+$text+"</option>";
	
					$html_result += $option_html;
					$html_result += "</select>";
				}

				$("#json_"+$child_name).html($html_result);
				//清空depth以上的SELECT
				if ($depth == 1) {
					$("#json_"+$jname+($depth+2)).html(""); //第三级
					$("#json_"+$jname+($depth+3)).html(""); //第四级
				}
				else if ($depth == 2) {
					$("#json_"+$jname+($depth+2)).html(""); //第四级
				}
				
				if ($callback.length > 0) { //回调函数
					$fun = eval($callback);
					if (typeof($fun) == "function") {
						$fun.call(this);
					}
				}

			},
			error: function() { }
		});
	}

}


//获取select选框子地区$obj->当前select的名称 不含 depth
function oeloveGetChildHometown($jname, $depth, $child, $text, $callback) {
	if (typeof($child) == "undefined") {$child = 0;}
	if (typeof($text) == "undefined") {$text = "";}
	if (typeof($callback) == "undefined") {$callback = "";}
	$depth = parseInt($depth);
	
	if ($depth < 4) {
		$j_obj = $("#"+$jname+$depth); //当前SELECT的OBJ
		$rootid = $j_obj.find("option:selected").val(); //当前SELECT所选的值

		if ($depth > 0) {
			if ($rootid < 1) { //加载子地区必选父地区
				//清空depth以上的SELECT
				if ($depth == 1) {
					$("#json_"+$jname+($depth+1)).html("<select name='"+$jname+"2'><option value=''>"+$text+"</option></select>"); //2
					$("#json_"+$jname+($depth+2)).html("<select name='"+$jname+"3'><option value=''>"+$text+"</option></select>"); //3
					$("#json_"+$jname+($depth+3)).html(""); //4
				}
				else if ($depth == 2) {
					$("#json_"+$jname+($depth+1)).html("<select name='"+$jname+"3'><option value=''>"+$text+"</option></select>"); //3
					$("#json_"+$jname+($depth+2)).html(""); //4
				}
				else {
					$("#json_"+$jname+($depth+1)).html(""); //4
				}
				return;
			}
		}
		//获取子地区
		$.ajax({
			type: "POST",
			url: _ROOT_PATH+"index.php",
			cache: false,
			data: {m:"index", c:"area", a:"hometown", parentid:$rootid, depth:$depth, r:get_rndnum(8)},
			dataType: "json",
			success: function($data) {
				$option_html = "";
				$.each($data, function($entryIndex, $obj){
					$option_html += "<option value='"+$obj.areaid+"'>"+$obj.areaname+"</option>";
				});
				//HTML
				$child_name = $jname+""+($depth+1);
				$html_result = "";
				if ($option_html.length > 0) { //存在option
					$html_result += "<select name='"+$child_name+"' id='"+$child_name+"'";
					if ($child > 0) {
						
						if ($callback.length > 0) {
							$html_result += " onchange=\"oeloveGetChildHometown('"+$jname+"', '"+($depth+1)+"', '"+$child+"', '"+$text+"', '"+$callback+"')\"";
						}
						else {
							$html_result += " onchange=\"oeloveGetChildHometown('"+$jname+"', '"+($depth+1)+"', '"+$child+"', '"+$text+"')\"";
						}
					}
					$html_result +=">";
					$html_result += "<option value=''>"+$text+"</option>";
	
					$html_result += $option_html;
					$html_result += "</select>";
				}

				$("#json_"+$child_name).html($html_result);
				//清空depth以上的SELECT
				if ($depth == 1) {
					$("#json_"+$jname+($depth+2)).html(""); //第三级
					$("#json_"+$jname+($depth+3)).html(""); //第四级
				}
				else if ($depth == 2) {
					$("#json_"+$jname+($depth+2)).html(""); //第四级
				}
				
				if ($callback.length > 0) { //回调函数
					$fun = eval($callback);
					if (typeof($fun) == "function") {
						$fun.call(this);
					}
				}

			},
			error: function() { }
		});
	}

}


